package com.artqiyi.dollmachine.views.fragment;

import android.graphics.PointF;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseFragment;
import com.artqiyi.dollmachine.common.constant.Constant;
import com.artqiyi.dollmachine.presenter.base.BasePresenter;
import com.artqiyi.dollmachine.utils.BitmapUtil;
import com.artqiyi.dollmachine.views.widget.ToolBarView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

import java.io.File;

import butterknife.Bind;
import timber.log.Timber;

/**
 * author: PengCheng
 * time: 2017/12/29 0029
 * desc: 操作界面详情
 */

public class OperateDetailFragment extends BaseFragment {

    @Bind(R.id.imageView)
    SubsamplingScaleImageView imageView;
    @Bind(R.id.tvEmpty)
    TextView tvEmpty;

    private String machineDetailImage;
    @Override
    protected void handleArguments(Bundle bundle) {
        super.handleArguments(bundle);
        machineDetailImage = getArguments().getString(Constant.KEY_MACHINE_DETAIL_IMAGE);
    }

    @Override
    protected BasePresenter getPresenter() {
        return new BasePresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_operate_detail;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tvEmpty.setVisibility(TextUtils.isEmpty(machineDetailImage)?View.VISIBLE: View.GONE);
        imageView.setVisibility(TextUtils.isEmpty(machineDetailImage)?View.GONE: View.VISIBLE);
        imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
        Glide.with(this).load(machineDetailImage).downloadOnly(new SimpleTarget<File>() {
            @Override
            public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
                int[] size = BitmapUtil.getBitmapSize(resource);
                float scale = imageView.getWidth() / (float) size[0];
                imageView.setImage(ImageSource.uri(Uri.fromFile(resource)), new ImageViewState(scale, new PointF(0, 0), 0));
            }
        });
//        Glide.with(this).load(machineDetailImage).placeholder(R.mipmap.img_fail).into(imageView);
    }

    @Override
    protected void handleToolBar(ToolBarView toolBar) {
        super.handleToolBar(toolBar);
        toolBar.setVisibility(View.GONE);
    }
}
